﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Tickz.Commonz
{
    /// <summary>
    /// 验证类
    /// </summary>
    public class Validator<T> : ISubject
    {
        #region 私有成员
        /// <summary>
        /// 验证器正则表达式规则
        /// </summary>
        private string[] validatorRegexs = new string[17];

        /// <summary>
        /// 是否为必填项
        /// </summary>
        private bool required;

        /// <summary>
        /// json字符串
        /// </summary>
        private StringBuilder tempJsonString;

        /// <summary>
        /// 验证器名称
        /// </summary>
        private string name;

        /// <summary>
        /// 是否验证通过
        /// </summary>
        private bool isPass;

        /// <summary>
        /// 是否需要验证
        /// </summary>
        private bool ignore;

        /// <summary>
        /// 泛型对象
        /// </summary>
        private T validateObj;

        /// <summary>
        /// 验证组名
        /// </summary>
        private string groupName;

        /// <summary>
        /// 错误信息列表
        /// </summary>
        private List<string> errorMessageList;

        /// <summary>
        /// 当前错误
        /// </summary>
        private string currentError;

        /// <summary>
        /// 自定义错误控件显示id
        /// </summary>
        private string customErrorID;

        /// <summary>
        /// 客户端json代码列表
        /// </summary>
        private List<string> jsonList;

        /// <summary>
        /// 完整的的json字符串
        /// </summary>
        private string jsonString;

        /// <summary>
        /// 对应客户端控件的ID
        /// </summary>
        private string clientID;
        #endregion

        #region 属性成员
        /// <summary>
        /// 是否为必填项
        /// </summary>
        public bool Required
        {
            get { return required; }
            set { required = value; }
        }

        /// <summary>
        /// 验证器名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 是否通过验证
        /// </summary>
        public bool IsPass
        {
            get { return isPass; }
        }

        /// <summary>
        /// 是否需要验证
        /// </summary>
        public bool Ignore
        {
            get { return ignore; }
            set { ignore = value; }
        }

        /// <summary>
        /// 所属组名
        /// </summary>
        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; }
        }

        /// <summary>
        /// 错误信息列表
        /// </summary>
        public List<string> ErrorMessageList
        {
            get { return errorMessageList; }
            set { errorMessageList = value; }
        }

        /// <summary>
        /// 当前错误
        /// </summary>
        public string CurrentError
        {
            get { return currentError; }
            set { currentError = value; }
        }

        /// <summary>
        /// 自定义错误控件显示id
        /// </summary>
        public string CustomErrorID
        {
            get { return customErrorID; }
            set { customErrorID = value; }
        }

        /// <summary>
        /// 客户端json代码列表
        /// </summary>
        public List<string> JsonList
        {
            get { return jsonList; }
            set { jsonList = value; }
        }

        /// <summary>
        /// 完整的的json字符串
        /// </summary>
        public string JsonString
        {
            get { return jsonString; }
            set { jsonString = value; }
        }

        /// <summary>
        /// 对应客户端控件的ID
        /// </summary>
        public string ClientID
        {
            get { return clientID; }
            set { clientID = value; }
        }
        #endregion

        #region  构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public Validator(T _validateObj)
            : this(_validateObj, "Default")
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_validateObj">待验证对象</param>
        /// <param name="_groupName">验证对象所在组</param>
        public Validator(T _validateObj, string _groupName)
            : this(_validateObj, _groupName, string.Empty)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_validateObj">待验证对象</param>
        /// <param name="_groupName">验证对象所在组</param>
        /// <param name="_name">验证器名称</param>
        public Validator(T _validateObj, string _groupName, string _name)
        {
            this.validateObj = _validateObj;
            this.groupName = _groupName;
            this.ignore = false;
            this.isPass = true;
            this.name = _name;
            jsonList = new List<string>();
            errorMessageList = new List<string>();
            required = true;

            #region 初始化验证器正则表达式规则
            //是否存在SQL注入字符
            validatorRegexs[0] = @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']";
            //验证合法的IP地址
            validatorRegexs[1] = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
            //验证是否为数字
            validatorRegexs[2] = @"^[-\+]?\d+(\.\d+)?$";
            //验证中文
            validatorRegexs[3] = @"^[\u0100-\uFFFF]+";
            //验证英文
            validatorRegexs[4] = @"^[A-Za-z]+$";
            //验证整数
            validatorRegexs[5] = @"^[-\+]?\d+$";
            //验证非负整数
            validatorRegexs[6] = @"^\d+$";
            //验证货币
            validatorRegexs[7] = @"^\d+(\.\d+)?$";
            //验证Url地址
            validatorRegexs[8] = @"^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\""\""])*$";
            //验证固定电话
            validatorRegexs[9] = @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$";
            //验证移动电话 13* 15* 18*
            validatorRegexs[10] = @"^((\(\d{2,3}\))|(\d{3}\-))?1[3|5|8]\d{9}$";
            //验证QQ号码
            validatorRegexs[11] = @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$";
            //验证邮编
            validatorRegexs[12] = @"^[1-9]\d{5}$";
            //验证固定电话
            validatorRegexs[13] = @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$";
            //验证固定电话
            validatorRegexs[14] = @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$";
            //验证固定电话
            validatorRegexs[15] = @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$";
            //验证Email
            validatorRegexs[16] = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";

            #endregion
        }
        #endregion

        #region SQL注入验证
        /// <summary>
        /// SQL注入验证
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public Validator<T> SQL(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            //生成客户端验证json结构体
            tempJsonString.Append("SQL:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[0]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region IP验证
        /// <summary>
        /// IP验证
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public Validator<T> IP(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            //生成客户端验证json结构体
            tempJsonString.Append("IP:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[1]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region 长度验证
        /// <summary>
        /// 长度验证
        /// </summary>
        /// <param name="min">最小字节数</param>
        /// <param name="max">最大字节数</param>
        /// <param name="errorMessage">错误提示信息</param>
        /// <returns>验证器</returns>
        public Validator<T> Length(int min, int max, string errorMessage)
        {
            tempJsonString = new StringBuilder();
            //生成客户端验证json结构体
            tempJsonString.Append("Length:{");
            tempJsonString.Append(ConvertToJsonElement("Min", min.ToString()));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Max", max.ToString()));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            int tempLength = validateObj.ToString().Length;
            if (tempLength > max || tempLength < min)
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region 范围验证
        /// <summary>
        /// 数值范围验证
        /// </summary>
        /// <param name="min">数值的最小值</param>
        /// <param name="max">数值的最大值</param>
        /// <param name="errorMessage">错误提示信息</param>
        /// <returns>验证器</returns>
        public Validator<T> Range(float min, float max, string errorMessage)
        {
            tempJsonString = new StringBuilder();
            //生成客户端验证json结构体
            tempJsonString.Append("Range:{");
            tempJsonString.Append(ConvertToJsonElement("Min", min.ToString()));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Max", max.ToString()));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Type", "Number"));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            try
            {
                float tempFloat = float.Parse(validateObj.ToString());
                if (tempFloat > max || tempFloat < min)
                {
                    this.Reject(errorMessage);
                }
            }
            catch
            {
                this.Reject(errorMessage);
            }

            return this;
        }

        /// <summary>
        /// 日期范围验证
        /// </summary>
        /// <param name="min">日期的最小值</param>
        /// <param name="max">日期的最大值</param>
        /// <param name="errorMessage">错误提示信息</param>
        /// <returns>验证器</returns>
        public Validator<T> Range(DateTime min, DateTime max, string errorMessage)
        {
            tempJsonString = new StringBuilder();
            //生成客户端验证json结构体
            tempJsonString.Append("Range:{");
            tempJsonString.Append(ConvertToJsonElement("Min", min.ToString()));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Max", max.ToString()));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Type", "Date"));
            tempJsonString.Append(",");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            try
            {
                DateTime tempDate = DateTime.Parse(validateObj.ToString());
                if (tempDate > max || tempDate < min)
                {
                    this.Reject(errorMessage);
                }
            }
            catch
            {
                this.Reject(errorMessage);
            }

            return this;
        }

        #endregion

        #region 重复数据一致性验证Repeat
        /// <summary>
        /// 重复数据一致性验证
        /// </summary>
        /// <param name="to">匹配的控件</param>
        /// <param name="errorMessage">错误提示信息</param>
        /// <returns>验证器</returns>
        public Validator<T> Repeat(string to, string errorMessage)
        {
            tempJsonString = new StringBuilder();
            //生成客户端验证json结构体
            tempJsonString.Append("Repeat:{");
            tempJsonString.Append(ConvertToJsonElement("To", to.ToString()));
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            return this;
        }
        #endregion

        #region Chinese中文字符验证
        /// <summary>
        /// Chinese中文字符验证
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Chinese(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Chinese:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (Regex.IsMatch(validateObj.ToString(), validatorRegexs[3], RegexOptions.IgnoreCase))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region English英文字符验证
        /// <summary>
        /// English英文字符验证
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> English(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("English:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[4]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }

        #endregion

        #region Integer整数验证
        /// <summary>
        /// Integer整数验证
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Integer(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Integer:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[5]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }

        #endregion

        #region 验证非负整数
        /// <summary>
        /// 验证非负整数
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Number(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Number:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[6]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }


        #endregion

        #region 验证数字(int,double)
        /// <summary>
        /// 验证数字(int,double)
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Double(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Double:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[2]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }

        #endregion

        #region 验证货币
        /// <summary>
        /// 验证货币
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Currency(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Currency:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[7]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }


        #endregion

        #region 验证邮箱格式
        /// <summary>
        /// 验证邮箱格式
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Email(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Email:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[16]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  验证Url地址
        /// <summary>
        ///  验证Url地址
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> URL(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("URL:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[8]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  日期格式验证
        /// <summary>
        ///  日期格式验证
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Date(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Date:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            try
            {
                DateTime.Parse(validateObj.ToString());
            }
            catch
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  验证固定电话
        /// <summary>
        ///  验证固定电话
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Phone(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Phone:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[9]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  验证移动电话
        /// <summary>
        ///  验证移动电话
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Mobile(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Mobile:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[10]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  验证移动电话和固定电话
        /// <summary>
        ///  验证移动电话和固定电话
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> MobileOrPhone(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("MobileOrPhone:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[10]) && IsPattern(validatorRegexs[9]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  验证QQ号码
        /// <summary>
        ///  验证QQ号码
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> QQ(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("QQ:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[11]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  验证邮编
        /// <summary>
        ///  验证邮编
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Zip(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("QQ:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(validatorRegexs[12]))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region  身份证号码验证
        /// <summary>
        ///  身份证号码验证
        /// </summary>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> IdCard(string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("IdCard:{");
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!CheckIDCard(validateObj.ToString()))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #region 验证身份证号
        /// <summary>
        /// 验证身份证号
        /// </summary>
        /// <param name="Id">待验证身份证字符串</param>
        /// <returns>是否合法</returns>
        private bool CheckIDCard(string Id)
        {
            int intLen = Id.Length;
            long n = 0;

            if (intLen == 18)
            {
                if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
                {
                    return false;//数字验证
                }
                string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
                if (address.IndexOf(Id.Remove(2)) == -1)
                {
                    return false;//省份验证
                }
                string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
                DateTime time = new DateTime();
                if (DateTime.TryParse(birth, out time) == false)
                {
                    return false;//生日验证
                }
                string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
                string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
                char[] Ai = Id.Remove(17).ToCharArray();
                int sum = 0;
                for (int i = 0; i < 17; i++)
                {
                    sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
                }
                int y = -1;
                Math.DivRem(sum, 11, out y);
                if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
                {
                    return false;//校验码验证
                }
                return true;//符合GB11643-1999标准
            }
            else if (intLen == 15)
            {
                if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
                {
                    return false;//数字验证
                }
                string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
                if (address.IndexOf(Id.Remove(2)) == -1)
                {
                    return false;//省份验证
                }
                string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
                DateTime time = new DateTime();
                if (DateTime.TryParse(birth, out time) == false)
                {
                    return false;//生日验证
                }
                return true;//符合15位身份证标准
            }
            else
            {
                return false;//位数不对
            }
        }

        #endregion
        #endregion

        #region  自定义正则表达式验证
        /// <summary>
        ///  自定义验证
        /// </summary>
        /// <param name="regexString">自定义正则表达式</param>
        /// <param name="errorMessage">错误提示字符串</param>
        /// <returns></returns>
        public Validator<T> Custom(string regexString, string errorMessage)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append("Custom:{");
            tempJsonString.Append(ConvertToJsonElement("RegexStr", regexString));
            tempJsonString.Append(ConvertToJsonElement("Msg", errorMessage));
            tempJsonString.Append("}");
            jsonList.Add(tempJsonString.ToString());
            //判断是否需要忽略验证
            if (ignore)
                return this;
            //验证
            if (!IsPattern(regexString))
            {
                this.Reject(errorMessage);
            }
            return this;
        }
        #endregion

        #region 检查是否匹配（正则验证）
        /// <summary>
        /// 检查是否匹配
        /// </summary>
        /// <param name="regexString"></param>
        /// <returns></returns>
        private bool IsPattern(string regexString)
        {
            //判断是否为必填项，若非必填项且值为空时，忽略该验证器
            if (validateObj.ToString().Trim() == "" && this.required == false)
            {
                this.ignore = true;
                return true;
            }
            return Regex.IsMatch(validateObj.ToString(), regexString, RegexOptions.IgnoreCase);
        }
        #endregion

        #region 设置验证器的分组
        /// <summary>
        /// 设置验证器的分组
        /// </summary>
        /// <param name="_groupName">组名</param>
        /// <returns>验证器对象</returns>
        public Validator<T> SetGroupName(string _groupName)
        {
            this.groupName = _groupName;
            return this;
        }
        #endregion

        #region 是否为必填项
        /// <summary>
        /// 是否为必填项
        /// </summary>
        /// <param name="isRequired"></param>
        /// <returns></returns>
        public Validator<T> IsRequired(bool isRequired)
        {
            this.required = isRequired;
            return this;
        }
        #endregion

        #region 注册客户端验证

        /// <summary>
        /// 注册客户端验证
        /// </summary>
        /// <param name="clientID">客户端控件id</param>
        /// <returns></returns>
        public Validator<T> RegClientValidator(string _clientID)
        {
            return RegClientValidator(_clientID, null);
        }

        /// <summary>
        /// 注册客户端验证
        /// </summary>
        /// <param name="clientID">客户端控件id</param>
        public Validator<T> RegClientValidator(string _clientID, string _tipHTML)
        {
            return RegClientValidator(_clientID, _tipHTML, null);
        }

        /// <summary>
        /// 注册客户端验证
        /// </summary>
        /// <param name="clientID">客户端控件id</param>
        /// <param name="tipHTML">输入提示信息</param>
        public Validator<T> RegClientValidator(string _clientID, string _tipHTML, string _errorContainerID)
        {
            this.clientID = _clientID;
            this.customErrorID = _errorContainerID;
            if (!string.IsNullOrEmpty(this.customErrorID))
            {
                jsonList.Add(ConvertToJsonElement("ErrorID", _errorContainerID));
            }
            this.customErrorID = _errorContainerID;
            //设置提示字符串
            if (!string.IsNullOrEmpty(_tipHTML))
                jsonList.Add(ConvertToJsonElement("Tip", _tipHTML));
            //设置是否为必填项难证
            if (!required)
            {
                jsonList.Add(ConvertToJsonElement("Required", "false"));
            }
            jsonString = ConnectJson(clientID);
            return this;
        }

        #endregion

        #region 返回验证后的值
        /// <summary>
        /// 返回数验证后结果
        /// </summary>
        /// <returns></returns>
        public T Retrun()
        {
            return Retrun(default(T));
        }

        /// <summary>
        /// 返回验证结果，若不通过使用自定义值
        /// </summary>
        /// <param name="defautlValue">自定义值</param>
        /// <returns></returns>
        public T Retrun(T defautlValue)
        {
            if (isPass)
                return this.validateObj;
            return defautlValue;

        }
        #endregion

        #region 静态调用
        /// <summary>
        /// 返回验证器对象
        /// </summary>
        /// <param name="_validateObj">待验证对象</param>
        /// <returns></returns>
        public static Validator<T> Init(T _validateObj)
        {
            return Init(_validateObj, "Default");
        }

        /// <summary>
        /// _validateObj
        /// </summary>
        /// <param name="_validateObj">待验证对象</param>
        /// <param name="_groupName">组名</param>
        /// <returns></returns>
        public static Validator<T> Init(T _validateObj, string _groupName)
        {
            return new Validator<T>(_validateObj, _groupName);
        }

        /// <summary>
        /// _validateObj
        /// </summary>
        /// <param name="_validateObj">待验证对象</param>
        /// <param name="_groupName">组名</param>
        /// <param name="_name">验证器名</param>
        /// <returns></returns>
        public static Validator<T> Init(T _validateObj, string _groupName, string _name)
        {
            return new Validator<T>(_validateObj, _groupName, _name);
        }
        #endregion

        #region 生成客户端json格式代码
        /// <summary>
        /// 组成json数据元素结构
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="value">值</param>
        /// <returns>键值对字符串</returns>
        private string ConvertToJsonElement(string key, string value)
        {
            return key + ":'" + value + "'";
        }

        /// <summary>
        /// 连接成组合的json字符串
        /// </summary>
        /// <param name="clientID">客户端控件id</param>
        /// <returns></returns>
        private string ConnectJson(string clientID)
        {
            tempJsonString = new StringBuilder();
            tempJsonString.Append(clientID + ":{");
            int i = 0;
            foreach (string s in jsonList)
            {
                i++;
                tempJsonString.Append(s);
                if (i != jsonList.Count)
                    tempJsonString.Append(",");

            }
            tempJsonString.Append("}");
            return tempJsonString.ToString();
        }

        #endregion

        #region 验证不通过处理函数
        /// <summary>
        ///  验证不通过处理函数
        /// </summary>
        /// <param name="errorMessage">错误信息</param>
        public void Reject(string errorMessage)
        {
            this.isPass = false;
            this.errorMessageList.Add(errorMessage);
            this.ignore = true;
            this.CurrentError = errorMessage;
        }
        #endregion
    }
}
